package com.sky.service.impl;

import com.sky.context.UserHolder;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;//购物车

    @Autowired
    private SetmealMapper setmealMapper;//套餐

    @Autowired
    private DishMapper dishMapper;//菜品

    //添加购物车
    @Override
    public void save(ShoppingCart shoppingCart) {

        //1.先根据用户的id+套餐或菜品从购物车查询
        shoppingCart.setUserId(UserHolder.get());//设置用户id
        ShoppingCart shoppingCartDB = shoppingCartMapper.findOne(shoppingCart);

        //2.判断
        if (shoppingCartDB != null) {

            //3.如果查询到了,让number+1,
            shoppingCartDB.setNumber(shoppingCartDB.getNumber() + 1);
            //3-2再更新回去
            shoppingCartMapper.update(shoppingCartDB);
        } else {
            //4.如果没查到,代表没有这个菜品或套餐
            if (shoppingCart.getSetmealId()!=null) {
                //套餐
                //4-1 先根据套餐id,查询套餐基本信息
                SetmealVO setmealVO = setmealMapper.findById(shoppingCart.getSetmealId());
                //4-2 补齐套餐id
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //4-3 保存
                shoppingCartMapper.insert(shoppingCart);
            }else {
                //5.菜品
                //5-1现根据菜品id,查询菜品的基本信息
                DishVO dishVO = dishMapper.findById(shoppingCart.getDishId());
                //5-2补齐参数
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //5-3保存
                shoppingCartMapper.insert(shoppingCart);
            }
        }

    }

    //查询购物车
    @Override
    public List<ShoppingCart> findList() {
        //根据用户的id进行查询
        Long aLong = UserHolder.get();

        return shoppingCartMapper.finByUserId(aLong);
    }

    //清空购物车
    @Override
    public void clean() {
        //根据用户的id进行删除
        Long aLong = UserHolder.get();

        shoppingCartMapper.deleteByUserId(aLong);
    }

    //删除购物车中一个商品
    @Override
    public void deleteById(ShoppingCart shoppingCart) {
        //1.根据当前用户查询是否点过 当前菜品或者是套餐
        shoppingCart.setUserId(UserHolder.get());//设置用户id
        ShoppingCart shoppingCartFromDB = shoppingCartMapper.findOne(shoppingCart);
        //2.判断
        if (shoppingCartFromDB != null) {
            if (shoppingCartFromDB.getNumber() == 1) {
                //3-1 点过，数量-1
                shoppingCartMapper.deleteByUserIdAndDishId(shoppingCart);
            }
            //3-2 点过，数量-1
            shoppingCartFromDB.setNumber(shoppingCartFromDB.getNumber() - 1);//数量-1
            //3-3 更新
            shoppingCartMapper.update(shoppingCartFromDB);
        }
    }
}
